/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.controllers;

import com.mycompany.psc.ejb.ProjectsManager;
import com.mycompany.psc.ejb.UsersManager;
import com.mycompany.psc.entities.Projects;
import com.mycompany.psc.entities.Users;
import com.mycompany.psc.producers.LoggedIn;
import java.io.FileInputStream;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author A7med
 */
@Named
@SessionScoped
public class ProjectsBean implements Serializable {

    private List<Users> available;
    private String projectName;
    private String projectDesc;
    private UploadedFile file;
    @Inject
    FilesBean fb;
    @EJB
    UsersManager usersManager;
    @EJB
    ProjectsManager projectsManager;
    private Projects selectedProject;
    private Projects selectedEditProject;
    private String oldProjectName;
    private Projects selectedReportProject;
    private Projects importFromProject;

    public Projects getSelectedEditProject() {
        return selectedEditProject;
    }

    public void setSelectedEditProject(Projects selectedEditProject) {
        this.selectedEditProject = selectedEditProject;
    }

    public String getOldProjectName() {
        return oldProjectName;
    }

    public void setOldProjectName(String oldProjectName) {
        this.oldProjectName = oldProjectName;
    }

    public Projects getImportFromProject() {
        return importFromProject;
    }

    public void setImportFromProject(Projects importFromProject) {
        this.importFromProject = importFromProject;
    }

    public Projects getSelectedReportProject() {
        return selectedReportProject;
    }

    public void setSelectedReportProject(Projects selectedReportProject) {
        this.selectedReportProject = selectedReportProject;
    }

    public StreamedContent getLogo() {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
            String path = context.getExternalContext().getRequestParameterMap().get("projectName");
            if (path != null) {
                System.out.println("getting image;");
                return fb.getImage(path);
            } else {
                return new DefaultStreamedContent();
            }
        } else {
            // So, browser is requesting the image. Get ID value from actual request param.
            String path = context.getExternalContext().getRequestParameterMap().get("projectName");
            System.out.println("getting image;");
            return fb.getImage(path);
        }
    }

    public void setLogo(StreamedContent logo) {
        this.logo = logo;
    }
    private StreamedContent logo;


    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    @PostConstruct
    public void init() {
        available = usersManager.getAllUsers();

    }

    public List<Users> getAvailable() {

        return available;
    }

    public List<Projects> listProjects() {
        return projectsManager.listAllProjects();
    }

    public void setAvailable(List<Users> available) {
        this.available = available;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectDesc() {
        return projectDesc;
    }

    public void setProjectDesc(String projectDesc) {
        this.projectDesc = projectDesc;
    }

    public void deleteProject() {
        projectsManager.deleteProject(selectedProject);
    }

    public void saveProject() {
        fb.createDirectory(projectName);
        System.out.println("file name=" + file.getFileName());
        fb.handleFileUpload(file, projectName);
        Projects p = projectsManager.SaveProject(projectName, projectDesc);
        System.out.println("project null=" + p == null);
        
    }

    public void updateProject() {
        if (!oldProjectName.equals(selectedEditProject.getName())) {
            fb.renameDirectory(oldProjectName, selectedEditProject.getName());
        }
        if (file != null) {
            fb.handleFileUpload(file, selectedEditProject.getName());
        }
        projectsManager.updateProject(selectedEditProject);

    }


    public void setUploadedFile(FileUploadEvent event) {
        file = event.getFile();
    }

    public String displayProject() {

        return "project?faces-redirect=true";
    }

    public String openProject() {
        selectedProject = null;
        selectedReportProject = null;
        return "index?faces-redirect=true";
    }

    public Projects getSelectedProject() {
        return selectedProject;
    }

    public void setSelectedProject(Projects selectedProject) {
        this.selectedProject = selectedProject;
    }

    public List<Projects> getOtherProjects(Projects p1) {
        return projectsManager.listOtherProjects(p1);
    }

    public void importTender() {
        System.out.println("importing");
        projectsManager.updateTenderItems(importFromProject, selectedReportProject);
    }
}
